package com.dhcc.sds.util;

import com.dhcc.core.framework.util.CommonUtil;
import com.googlecode.aviator.AviatorEvaluator;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhangdc
 * @version 1.0
 * @date 2021/6/3 11:35
 */
public class StrUtil {

    //是否包含字母
    private static String CONTAIN_LETTER_REGEX = ".*[a-zA-Z]+.*";

    //字符串是否包含字母
    public static boolean judgeContainsStr(String str){
        Matcher m = Pattern.compile(CONTAIN_LETTER_REGEX).matcher(str);
        return m.matches();
    }

    //对入组参数包含大于、小于的判断
    public static boolean judgeGreaterOrLess(String arg, Long dateCompare){
        boolean res = false;
        if (!StringUtils.isEmpty(arg)&&dateCompare!=null){
            String exp = dateCompare + arg;
            res = (boolean) AviatorEvaluator.execute(exp);

//            //大于或者小于符合
//            String substring = arg.substring(0, 1);
//            //数字
//            String substring2 = arg.substring(1, arg.length());
//            Long l = Long.parseLong(substring2);
//            if (Objects.equals("<", substring)){
//                if (dateCompare < l){
//                    res = true;
//                }else {
//                    res = false;
//                }
//            }else if (Objects.equals(">", substring)){
//                if (dateCompare > l){
//                    res = true;
//                }else {
//                    res = false;
//                }
//            }
        }
        return res;
    }

    public static long strToLong(String str){
        String orderAmount = str;
        Number num = Float.parseFloat(orderAmount)*1000;
        int oamount = num.intValue();
        long cost = Long.valueOf(oamount);
        return cost;
    }

    /**
     * @Description: 除法保留两位小数 无%
     */
    public static String deciFormatU(int a,int b){
        if(b==0) return "0.00";
        DecimalFormat df=new DecimalFormat("0.00");
        String value = df.format(((double)a/b));
        return value;

    }

    /**
     * @Description: 除法保留两位小数 无%
     */
    public static String deciFormatM(int a,int b){
        if(b==0) return "0.00";
        DecimalFormat df=new DecimalFormat("0.00");
        String value = df.format(((double)a/b)*100);
        return value;

    }

    /**
     * 除法保留两位小数
     * @param a
     * @param b
     * @return
     */
    public static String deciFormat(int a,int b){
        if(b==0) return "0.00%";
        DecimalFormat df=new DecimalFormat("0.00");
        String value = df.format(((double)a/b)*100);
        return value+"%";

    }

    /**
     * @Description: 除法保留两位小数 无%
     * @Title: deciFormatU
     * @return: String
     */
    public static String deciFormatDouble(double a,int b){
        if(b==0) return "0.00";
        DecimalFormat df=new DecimalFormat("0.00");
        String value = df.format(((double)a/b));
        return value;

    }

    /**
     * @Description: 除法保留两位小数 无%
     * @Title: deciFormatLong
     * @return: String
     */
    public static String deciFormatLong(Long a,int b){
        if(b==0) return "0.00";
        DecimalFormat df=new DecimalFormat("0.00");
        String value = df.format(((double)a/b));
        return value;

    }

    /**
     * 取值为y显示1
     * @param s
     * @return
     */
    public static int getSdsFormValue(String s){
        if (Objects.equals("y", s)){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * @Description: 计算上报环比 有%
     * 环比增长率＝（本期数－上期数）/上期数*100％
     */
    public static String ringRatio(int a,int b){
        if(b==0) return "0.00%";
        DecimalFormat df=new DecimalFormat("0.00");
        String value = df.format(((double)(a - b)/b)*100);
        return value+"%";

    }

    /**
     * @Description: 计算数值是否在某个范围内
     */
    public static Boolean valueInRange(String a,String b){
        String[] split = a.split("-");
        String s = split[0];
        String e = split[1];
        int s1 = Integer.parseInt(s);
        int e1 = Integer.parseInt(e);
        int b1 = Integer.parseInt(b);
        if (b1 >= s1 && b1 <= e1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 移除字符串中非法字符
     * @param targetStr
     * @return
     */
    public static String clearSpecialChar(String targetStr) {
        if (CommonUtil.isNotEmpty(targetStr)) {
            targetStr = targetStr.replaceAll("[\t\n\r]","");// 换行符、制表符、回车符
            targetStr = targetStr.replaceAll("\"",""); // 半角双引号
            targetStr = targetStr.replaceAll("\'",""); // 半角单引号
            targetStr = targetStr.replaceAll("\\\\",""); // 反斜杠
        }
        return targetStr;
    }

}
